<template>
  <div>
    <el-form
      ref="lmform"
      label-width="100px"
      :model="form"
      :rules="rules"
      status-icon
    >
      <el-form-item label="登录名" prop="loginName">
        <el-input
          v-model="form.loginName"
          placeholder="请输入登录名"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="save()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "UserEdit",
  props: {
      dialogshow: {
          type: Boolean
      },
      editId: {
        type: Number,
        default: null
      }
  },
  data() {
    const checkdata = (rule,value,callback) => {
      const params = {value: value};
      if(this.editId) {
        params.id = this.editId
      }
      this.$axios.get('/ums-user/check',params,response => {
          if(response.obj > 0) {
              callback(new Error('数据已经存在，请重新输入'))
          } else {
              callback()
          }
      })
    }
    return {
      form: {
        loginName: "",
        name: "",
        phone: "",
        email: "",
      },
      rules: {
        loginName: [
          { required: true, message: "登录名不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "登录名长度小于20", trigger: "blur" },
          {validator: checkdata, trigger: "change"}
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "姓名长度小于20", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "手机号长度小于20", trigger: "blur" },
          {
            pattern: /^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$/,
            message: "手机号不合法",
            trigger: "blur",
          },
          {validator: checkdata, trigger: "change"}
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "邮箱长度小于50", trigger: "blur" },
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            message: "邮箱不合法",
            trigger: "blur",
          },
          {validator: checkdata, trigger: "change"}
        ],
      },
      url: '/ums-user/add'
    };
  },
  created() {
    if(this.editId) {
      this.url = '/ums-user/update'
      this.getById()
    }
  },
  methods: {
    save() {
      this.$axios.post(this.url, this.form, () => {
        this.$emit('update:dialogshow',false)
        this.$emit('getTable')
      },this.$refs['lmform'])
    },
    getById() {
      this.$axios.get('ums-user/getone',{id: this.editId}, response => {
        for(let key in this.form) {
          this.form[key] = response.obj[key]
        }
        this.form.id = response.obj.id
      })
    }
  },
};
</script>
<style lang="less" scoped>
</style>